package com.inspur.system.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.inspur.common.utils.ListUtils;
import com.inspur.common.utils.SecurityUtils;
import com.inspur.common.utils.StringUtils;
import com.inspur.common.utils.uuid.UUID;
import com.inspur.system.domain.vo.SysUserSaveProjVo;
import com.inspur.system.dto.PmProDTO;
import com.inspur.system.dto.ProNumDTO;
import com.inspur.system.dto.ProTextDTO;
import com.inspur.system.mapper.SysUserMapper;
import com.inspur.system.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.inspur.system.mapper.PmProMapper;
import com.inspur.system.domain.PmPro;
import com.inspur.system.service.IPmProService;

/**
 * 【项目】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class PmProServiceImpl implements IPmProService 
{
    @Autowired
    private PmProMapper pmProMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询【项目】
     * 
     * @param cProNo 【项目】主键
     * @return 【项目】
     */
    @Override
    public PmProDTO selectPmProByCProNo(String cProNo)
    {
        return pmProMapper.selectPmProByCProNo(cProNo);
    }

    /**
     * 查询【项目】列表
     * 
     * @param pmPro 【项目】
     * @return 【项目】
     */
    @Override
    public List<PmProDTO> selectPmProList(PmProDTO pmPro)
    {
        List<PmProDTO> pmProDTOList = new ArrayList<>();
        List<PmProDTO> pmPros = pmProMapper.selectPmProList(pmPro);
        pmPros.forEach(pro ->{
            ProNumDTO proNumDTO = this.selectPmProPace(pro.getcProNo());
            if (proNumDTO.getcProDou() != 0 && proNumDTO.getcProDou1() != 0) {
                BigDecimal b = new BigDecimal(proNumDTO.getcProDou1()/proNumDTO.getcProDou());
                pro.setcProPace(b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                pro.setcProPace(0d);
            }
            if (StringUtils.isNotEmpty(pro.getcPrdPer())) {
                pro.setcPerNum(UserUtils.queryUserNum(pro.getcPrdPer()));
            } else {
                pro.setcPerNum(0L);
            }
        });
        if (StringUtils.isNotEmpty(pmPro.getcProState())) {
            pmPros.forEach(pro -> {
                if (pmPro.getcProState().equals(pro.getcProState())) {
                    pmProDTOList.add(pro);
                }
            });
        } else {
            ListUtils.listAddAll(pmProDTOList,pmPros);
        }
        return pmProDTOList;
    }

    /**
     * 导出时查询【项目】列表
     *
     * @param pmPro 【项目】
     * @return 【项目】
     */
    @Override
    public List<PmPro> selectPmProListExport(PmPro pmPro)
    {
        return pmProMapper.selectPmProListExport(pmPro);
    }

    /**
     *  查询项目数量
     * */
    @Override
    public ProNumDTO selectPmProNum()
    {
        return pmProMapper.selectPmProNum();
    }

    @Override
    public ProNumDTO selectPmProWorkNum(String cProNo)
    {
        return pmProMapper.selectPmProWorkNum(cProNo);
    }

    /**
     * 查询项目工作项工时
     */
    @Override
    public ProNumDTO selectPmProWorkTime(String cProNo)
    {
        ProNumDTO proNumDTO = pmProMapper.selectPmProWorkTime(cProNo);
        if (proNumDTO != null) {
            if (proNumDTO.getcProDou1() != 0 && proNumDTO.getcProDou2() != 0) {
                BigDecimal b = new BigDecimal(proNumDTO.getcProDou1()/(proNumDTO.getcProDou1()+proNumDTO.getcProDou2()));
                proNumDTO.setcProDou3(b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                proNumDTO.setcProDou3(0d);
            }
            proNumDTO.setcProDou4(proNumDTO.getcProDou()-proNumDTO.getcProDou1()-proNumDTO.getcProDou2());
            BigDecimal b1 = new BigDecimal(proNumDTO.getcProDou4()/(proNumDTO.getcProDou1()+proNumDTO.getcProDou2()));
            proNumDTO.setcProDou5(b1.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
            return proNumDTO;
        } else {
            return new ProNumDTO();
        }

    }

    /**
     * 新增【项目】
     * 
     * @param pmPro 【新增项目】
     * @return 结果
     */
    @Override
    public int insertPmPro(PmPro pmPro)
    {
        String pmProNo = "10" + UUID.generateShortUuid();
        pmPro.setcProNo(pmProNo);
        Long userId = SecurityUtils.getUserId();
        pmPro.setcCrePer(userId+"");
        pmPro.setcUpdPer(userId+"");
        pmPro.settCreTm(new Date());
        pmPro.settUpdTm(new Date());
        // 同步用户表
        List<SysUserSaveProjVo> users = UserUtils.updatePmNo(userId,pmPro.getcPrdPer(),pmPro.getcProNo(),pmPro.getcProHead());
        users.forEach(user -> userMapper.updateUserByProj(user));
        return pmProMapper.insertPmPro(pmPro);
    }

    /**
     * 修改【项目】
     * 
     * @param pmPro 【项目】
     * @return 结果
     */
    @Override
    public int updatePmPro(PmPro pmPro)
    {
        return pmProMapper.updatePmPro(pmPro);
    }

    /**
     * 批量删除【项目】
     * 
     * @param cProNos 需要删除的【项目】主键
     * @return 结果
     */
    @Override
    public int deletePmProByCProNos(String[] cProNos)
    {
        return pmProMapper.deletePmProByCProNos(cProNos);
    }

    /**
     * 删除【项目】信息
     * 
     * @param cProNo 【项目】主键
     * @return 结果
     */
    @Override
    public int deletePmProByCProNo(Long cProNo)
    {
        return pmProMapper.deletePmProByCProNo(cProNo);
    }

    @Override
    public ProNumDTO selectPmProPace(String cProNo)
    {
        return pmProMapper.selectPmProPace(cProNo);
    }

    @Override
    public HashMap<String,String> selectPmProName(String cProNo)
    {
        HashMap<String,String> map = new HashMap<>();
        List<PmPro> pmProList = pmProMapper.selectPmProName(cProNo);
        pmProList.forEach(pmPro -> map.put(pmPro.getcProNo(),pmPro.getcProNme()));
        return map;
    }

    /**
     * 查询燃尽图
     */
    @Override
    public HashMap<String,List> selectPmProWorkEchars(String cProNo)
    {
        HashMap<String,Object> map = new HashMap<>();
        HashMap<String,List> map1 = new HashMap<>();
        PmProDTO pmProDTO = new PmProDTO();
        pmProDTO.setcProNo(cProNo);
        pmProDTO.setcTable("PM_DEM");
        List<ProTextDTO> demList = pmProMapper.selectPmProWorkEchars(pmProDTO);
        pmProDTO.setcTable("PM_FLAW");
        List<ProTextDTO> flawList = pmProMapper.selectPmProWorkEchars(pmProDTO);
        pmProDTO.setcTable("PM_TASK");
        List<ProTextDTO> taskList = pmProMapper.selectPmProWorkEchars(pmProDTO);
        List<ProTextDTO> proTextDTOList = new ArrayList<>();
        ListUtils.listAddAll(proTextDTOList,demList);
        ListUtils.listAddAll(proTextDTOList,flawList);
        ListUtils.listAddAll(proTextDTOList,taskList);
        proTextDTOList.forEach(ProTextDTO -> map.merge(
                ProTextDTO.getcProText(),
                ProTextDTO.getcProText1(),
                (v1, v2) -> new BigDecimal(v1.toString()).add(new BigDecimal(v2.toString()))));
        TreeMap<String,Object> treeMap = new TreeMap<>(map);
        List<String> list = new ArrayList<>(treeMap.keySet());
        List<Object> list1 = new ArrayList<>(treeMap.values());
        map1.put("xList",list);
        map1.put("dataList",list1);
        return map1;
    }
}
